//
//  CLRunLoopThread.m
//  RunloopTest
//
//  Created by Charles on 2016/10/7.
//  Copyright © 2016年 Charles. All rights reserved.
//

#import "CLRunLoopThread.h"

#define kUseParticularModeToStartRunLoop    0

@implementation CLRunLoopThread{
    NSInteger _timerIndex;
}

- (void)main{
    @autoreleasepool {
        NSLog(@"Thread Enter");
        NSRunLoop *currentThreadRunLoop = [NSRunLoop currentRunLoop];
//        CFRunLoopRef currentThreadRunLoop = CFRunLoopGetCurrent();
        //创建一个 RunLoopObserver,并添加到当前RunLoop中,设置mode 为Default
        CFRunLoopObserverContext context = {0,(__bridge void *)(self),NULL,NULL,NULL};
        CFRunLoopObserverRef observer = CFRunLoopObserverCreate(kCFAllocatorDefault, kCFRunLoopAllActivities, YES, 0, &currentRunLoopObserver, &context);
        if (observer) {
            CFRunLoopRef runLoopRef = currentThreadRunLoop.getCFRunLoop;
            CFRunLoopAddObserver(runLoopRef, observer, kCFRunLoopDefaultMode);
        }
        
        //创建一个Timer,重复调用来驱动Run Loop
        [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(handleTimerTask) userInfo:nil repeats:YES];
        NSInteger loopCount = 10;
        //执行RunLoop 10次后退出,每次RunLoop返回的时候检查是否有线程退出的条件成立
        do {
            NSLog(@"LoopCount: %zi",loopCount);
            if (kUseParticularModeToStartRunLoop) {
                [currentThreadRunLoop runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantPast]];
            }else{
                [currentThreadRunLoop runUntilDate:[NSDate dateWithTimeIntervalSinceNow:1]];
            }
            loopCount --;
        } while (loopCount);
        NSLog(@"Thread Exit");
    }
}


#pragma mark - Observer CallBack


void currentRunLoopObserver(CFRunLoopObserverRef observer, CFRunLoopActivity activity, void *info)
{
    NSLog(@"Current thread Run Loop activity: %@", printActivity(activity));
}

static inline NSString* printActivity(CFRunLoopActivity activity)
{
    NSString *activityDescription;
    switch (activity) {
        case kCFRunLoopEntry:
            activityDescription = @"kCFRunLoopEntry";
            break;
        case kCFRunLoopBeforeTimers:
            activityDescription = @"kCFRunLoopBeforeTimers";
            break;
        case kCFRunLoopBeforeSources:
            activityDescription = @"kCFRunLoopBeforeSources";
            break;
        case kCFRunLoopBeforeWaiting:
            activityDescription = @"kCFRunLoopBeforeWaiting";
            break;
        case kCFRunLoopAfterWaiting:
            activityDescription = @"kCFRunLoopAfterWaiting";
            break;
        case kCFRunLoopExit:
            activityDescription = @"kCFRunLoopExit";
            break;
        default:
            break;
    }
    return activityDescription;
}

#pragma mark - Actions

- (void)handleTimerTask{
    NSLog(@"handleTimerTask");
    // 只有在上面do while循环中使用-runMode:beforDate才有效
    if (kUseParticularModeToStartRunLoop) {
        _timerIndex ++;
        NSLog(@"timer Index : %zi",_timerIndex);
        if (_timerIndex > 5) {
            //只有在-runMode:beforDate 和 -run 两种情况下有效
            CFRunLoopStop(CFRunLoopGetCurrent());
        }
    }
}

@end
